<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          default-expand-all
          :data="list"
          :props="{children: 'children',
                   label: 'name'}"
        >
          <!-- 使用作用域插槽获取组件内部的数据 -->
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="good" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- 新增子部门或者编辑部门弹层 -->
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :title="isEdit?'编辑部门':'新增子部门'"
        :visible.sync="showDialog"
        @close="hClose"
      >
        <addOrEdit v-if="showDialog" :id="curId" :is-edit="isEdit" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import addOrEdit from './deptDialog.vue'
import { getDepartments } from '@/api/departments'
import { transDataToTreeData } from '@/utils'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      list: [],
      showDialog: false, // 控制弹层的显示和隐藏
      curId: '',
      isEdit: false // false新增  ||  true 编辑
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hClose() {
      this.$refs.myAddRef.resetForm()
    },
    hSuccess() {
      // 重新渲染
      this.loadDepartments()
      // 关闭遮罩
      this.showDialog = false
    },
    hAdd(id) {
      this.isEdit = false
      this.showDialog = true
      // 更新当前操作的id
      this.curId = id
    },
    hEdit(id) {
      this.curId = id
      this.isEdit = false
      this.showDialog = true
    },
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log('getDepartments', res)
        res.data.depts.shift() // 删除第一个
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
